package TruongNhaLib;

import java.util.List;

import www.truongnha.com.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class HomeClassAdapter extends ArrayAdapter<Model> {
	final Toast toast = Toast.makeText(getContext(), "", Toast.LENGTH_SHORT);
	private final List<Model> list;
	private final Activity context;
	public HomeClassAdapter(Activity context, List<Model> list) {
		super(context, R.layout.home_class_item, list);
		this.context = context;
		this.list = list;
		Log.e("adapter", "constructor");
	}

	static class ViewHolder {
		protected TextView name;
		protected TextView status;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = null;
		
		if (convertView == null) {
			final int po = position;
			Log.e("adapter", "convertView == null");
			LayoutInflater inflator = context.getLayoutInflater();
			view = inflator.inflate(R.layout.home_class_item, null);
			final ViewHolder viewHolder = new ViewHolder();
			
			
			viewHolder.status =(TextView) view.findViewById(R.id.std_status);
			viewHolder.status.setText(list.get(position).getSex()+"-"+ list.get(position).getBirth());
			view.setTag(viewHolder);
			viewHolder.status.setTag(list.get(position));

			viewHolder.name = (TextView) view.findViewById(R.id.std_name);
			viewHolder.name.setText(list.get(position).getIndex()+"- "+list.get(position).getName());
			viewHolder.name.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					// TODO Auto-generated method stub
					AlertDialog alertDialog = new AlertDialog.Builder(context).create();
					alertDialog.setTitle(list.get(po).getIndex()+". "+ list.get(po).getName());
					alertDialog.setMessage("Ngày sinh: "+list.get(po).getBirth()+"\n"+"Giới tính : "+ list.get(po).getSex()+"\n" );
					alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
					      public void onClick(DialogInterface dialog, int which) {
					    	  dialog.cancel();
					    } });
					alertDialog.show();
				}
			});
			
		} else {
			Log.e("adapter", "use convertview ");
			view = convertView;
			((ViewHolder) view.getTag()).status.setTag(list.get(position));
		}//end if.
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.name.setText(list.get(position).getIndex()+". "+list.get(position).getName());
		return view;
		
	}
}

	